package com.xiyou.luckdraw.common.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CopyObjectResult;
import com.xiyou.luckdraw.common.enums.OssDirEnum;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.UUID;

/**
 * @ClassName: OssUtils
 * @Description:
 * @author: limingxing
 * @Date: 2019-10-10 19:41
 */
@Data
@Component
public class OssUtils {

 //   @Value("${ali.oss.key}")
    private String aliOssKeyId;//阿里oss的keyId
//    @Value("${ali.oss.secret}")
    private String aliOssKeySecret;//阿里oss的keySecret
 //   @Value("${ali.oss.bucket}")
    private String aliOssBucket;
//    @Value("${ali.oss.end.point}")
    private String aliEndPoint;

    private static OSSClient client = null;

    private String pref = "luckdraw/";

    /**
     * 获取OSSClient
     *
     * @return
     */
    public OSSClient getClient() {
        if (client == null) {
            client = new OSSClient(aliEndPoint, aliOssKeyId, aliOssKeySecret);
        }
        return client;
    }

    /**
     * 获取oss Url
     * @param oldUrl
     * @return
     */
    public String getUrl(String oldUrl) {
        if (!StringUtils.isEmpty(oldUrl)) {
            StringBuffer sb = new StringBuffer();
            Date expiration = new Date(new Date().getTime() + 360000 * 1000);
            String[] ss = oldUrl.split(",");
            for (String s : ss) {
                // 生成URL
                URL url = getClient().generatePresignedUrl(aliOssBucket, s, expiration);
                if (null != url) {
                    sb.append(url.toString()).append(",");
                }
            }
            return sb.toString().substring(0, sb.toString().lastIndexOf(","));
        }
        return oldUrl;
    }

    /**
     * 上传文件
     * @param dir
     * @param name
     * @param file
     * @date 2019-10-11 17:56
     */
    public String upload(OssDirEnum dir, String name, InputStream file) {
        if (StringUtils.isBlank(name)) {
            name = UUID.randomUUID().toString();
        }
        String key = pref + (null == dir ? OssDirEnum.DEFAULT.getDir() : dir.getDir()) + name;
        getClient().putObject(aliOssBucket, key, file);
        return key;
    }

    /**
     * 移动文件
     *
     * @param source
     * @param destination
     * @date 2019-10-12 20:37
     */
    public String move(String source, String destination) {
        boolean exist = getClient().doesObjectExist(aliOssBucket, source);
        if (exist) {
            CopyObjectResult request = getClient().copyObject(aliOssBucket, source, aliOssBucket, destination);
            getClient().deleteObject(aliOssBucket, source);
            return request.getETag();
        }
        return null;
    }

    /**
     * 复制文件
     * @param source
     * @param destination
     * @date 2019-10-12 20:37
     */
    public String copy(String source, String destination) {
        boolean exist = getClient().doesObjectExist(aliOssBucket, source);
        if (exist) {
            CopyObjectResult request = getClient().copyObject(aliOssBucket, source, aliOssBucket, destination);
            return destination;
        }
        return null;
    }


}
